package com.gree.scmservice.dao;

import com.gree.scmservice.entity.smbse.Smbse05;
import com.gree.scmservice.entity.smbse.Smbse08;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.TableName;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RoleDao {

    @Autowired
    private Dao dao;

    public int getRoleCount(String comp, String filter){
        Sql sql = Sqls.create("select count(*) as nums from $smbse05 sb05 " + filter);
        sql.setVar("smbse05","smbse05"+comp);
        sql.setCallback(Sqls.callback.entity());
        sql.setEntity(dao.getEntity(Record.class));
        dao.execute(sql);
        Record countRecord=(Record) sql.getResult();
        int count= countRecord.getInt("nums");
        return count;
    }

    public List<Smbse05> displayRole(String comp, String filter, int count, Pager pager){
        Sql sql = Sqls.create("select sb05.* from $smbse05 sb05 " + filter);
        sql.setVar("smbse05","smbse05"+comp);
        pager.setRecordCount(count);
        sql.setPager(pager);
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(Smbse05.class));
        List<Smbse05> dataList = dao.execute(sql).getList(Smbse05.class);
        return dataList;
    }

    public Smbse05 fetchSmbse05ById(String comp,String roleKey){
        TableName.set(comp);
        Smbse05 smbse05 = dao.fetch(Smbse05.class,Cnd.where("roid","=",roleKey));
        TableName.clear();
        return smbse05;
    }

    public List<Smbse05> insertSmbse05(String comp,List<Smbse05> smbse05s){
        TableName.set(comp);
        List<Smbse05> smbse05List = dao.insert(smbse05s);
        TableName.clear();
        return smbse05List;
    }

    public int updateSmbse05(String comp,List<Smbse05> smbse05s){
       TableName.set(comp);
       int num = dao.update(smbse05s);
       TableName.clear();
       return num;
    }

    public List<Smbse08> insertSmbse08s(String comp,List<Smbse08> smbse08s){
        TableName.set(comp);
        List<Smbse08> smbse08List = dao.insert(smbse08s);
        TableName.clear();
        return smbse08List;
    }

    public int deleteSmbse08sByTenos(String comp,String[] tenos){
        TableName.set(comp);
        int result = dao.clear(Smbse08.class,Cnd.where("teno","in",tenos));
        TableName.clear();
        return result;
    }

    public int deleteSmbse08sByRoids(String comp,String[] roids){
        TableName.set(comp);
        int result = dao.clear(Smbse08.class,Cnd.where("roid","in",roids));
        TableName.clear();
        return result;
    }

    public int deleteSmbse05ByRoleKeys(String comp,String[] roleKeys){
        TableName.set(comp);
        int result = dao.clear(Smbse05.class, Cnd.where("roid","in",roleKeys));
        TableName.clear();
        return result;
    }
}
